package fuza.android.thetrafficsigns;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

public class GridProhibitionSign extends Activity {

	SQLiteDatabase db;
	String n_gambar;
	GridView gridView;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		gridView = (GridView) findViewById(R.id.gridView1);
		List<String> li = new ArrayList<String>();
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_gallery_item,li);
		dataAdapter.setDropDownViewResource(R.layout.main);
		
		try {
			db = openOrCreateDatabase("trafficsigns.db", MODE_PRIVATE, null);
			Cursor cur = db.rawQuery("SELECT n_gambar FROM tb_rambu", null);
			if(cur !=null){
				if(cur.moveToFirst()){
					do{
					String gambar = cur.getString(cur.getColumnIndex("n_gambar"));
					li.add(gambar);
					gridView.setAdapter(dataAdapter);
				}
				while (cur.moveToNext());
				}
				else{
					Toast.makeText(getApplicationContext(),"gak ada data", Toast.LENGTH_LONG).show();
				}
			}
			cur.close();
			db.close();
		} catch (Exception e) {
			// TODO: handle exception
			Toast.makeText(getApplicationContext(),"Error" + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Toast.makeText(
						getApplicationContext(),
						position+ "##selected", Toast.LENGTH_SHORT).show();

			}

		});
		
		/*Bundle extras = getIntent().getExtras();
		String jenis = extras.getString("jenis");
		Toast.makeText(getApplicationContext(), "ini toast, udah masyuuk "+jenis,
				   Toast.LENGTH_LONG).show();*/

	}

}